// pages/detail/detail.js
import {getDetailInfo,
  getCommentList,
  getVideo
  } from '../../api/detail'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videoSrc: '', // 替换为实际的视频地址
    currentTab: 'chapter',//chapter,detail,evaluate
    detailInfo:null,
    chapterList:[],
    commentList:null,
    currentChapter:null,
    currentSubSectionId:-1,
    showMenu: false,
    courseId:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    await this.setData({courseId : options.courseId})
    await this.setData({currentSubSectionId : options.sectionId || -1})
    console.log('onload',this.data.currentSubSectionId)
    await this.getDetailInfo(this.data.courseId)
    // 设置子章节id，sectionId
    // 若未传递sectionId，则默认设置第一个视频

    await this.getCommentList(this.data.courseId)
    // 根据sectionId，获取视频资源
    await this.getVideo(this.data.currentSubSectionId)
  },
  async getDetailInfo(courseId){
    const res=await getDetailInfo(courseId)
    // 设置详情信息
    this.setData({detailInfo:res.data})
    // 设置章节列表
    this.setData({chapterList:res.data.sections})
    this.setData({courseId:res.data.courseId})
    this.setData({videoSrc:res.data.sections[0].subSections[0].file.fileName})
    if(this.data.currentSubSectionId == -1)
    this.setData({currentSubSectionId:this.data.detailInfo.sections[0].subSections[0].sectionId })
    console.log('this.setData({currentSubSectionI',this.data.currentSubSectionId)

  },
  async getCommentList(courseId){
    const commentList= await getCommentList(courseId)
    console.log('comment',commentList)
    this.setData({commentList : commentList.rows})
  },
  async getVideo(sectionId){
    const res = await getVideo(sectionId)
    this.setData({videoSrc:res.section.file.fileUrl})
  },
  switchTab: function(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({ currentTab: tab });
  },
  async handleChapter(e){
      // console.log('e.currentTarget.dataset.info',e.currentTarget.dataset.info)
      await this.setData({currentSubSectionId:e.currentTarget.dataset.info })
      // console.log('handleChapter,currentSubSectionId',this.data.currentSubSectionId)
      await this.getVideo(this.data.currentSubSectionId)
  },
  toggleMenu() {
    this.setData({
      showMenu: !this.data.showMenu,
    });
  },
  closeMenu() {
    this.setData({
      showMenu: false,
    });
  },
  navigateToSpeed() {
    wx.navigateTo({
      url: '/pages/speed/speed',
    });
    this.closeMenu();
  },

  navigateToComments() {
    wx.navigateTo({
      url: `/pages/comment/comment?courseId=${this.data.courseId}&sectionId=${this.data.currentSubSectionId}`,
    });
    this.closeMenu();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 重新进入页面，刷新评论
     this.getCommentList(this.data.courseId)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})